//
//  ViewController.swift
//  JRHttpRequestManager
//
//  Created by 京睿 on 2017/4/26.
//  Copyright © 2017年 JingRuiWangKe. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class ViewController: UIViewController {
    
    @IBOutlet weak var searchTextField: UITextField!
    
    let bag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let sg = searchTextField.rx.text
        sg
            .orEmpty
            .changed
            .debounce(0.3, scheduler: MainScheduler.instance)
            .distinctUntilChanged()
            .filter { $0.characters.count > 0 }
            .map { info in
                dump("搜索开始")
                return ["action": "searchWdAuthComplete", "areaId": "100210011001", "wd": info]
            }
            .flatMap { parameter in
                return JRHttpRequestManager.rxRequest(parameter)
            }
            .subscribe(
                onNext: {
                    print($0)
            }
                ,
                onError: {
                    if let err = $0 as? JRError {
                        switch err {
                        case .normal(let x):
                            print(x)
                        default :
                            print(err.localizedDescription)
                        }
                    }
            }
                ,
                onCompleted: {
                    print("Request completed")
            })
            .addDisposableTo(bag)
    }
}

